﻿using System;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Configuration;
using System.Net.Mail;

namespace CrawlServiceCore.Crawl
{
    /// <summary>
    /// An implementation of IHanldeCrawlInformation which send an email based on the SMTP details in the configuration file
    /// </summary>
    public class EmailHandler:IHandleCrawlInformation
    {
        public void SendInformation(string userName, string description, string url)
        {
            LogWriter log = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(ConfigurationManager.AppSettings["DataBaseToAddress"].ToString());
                mail.Subject = userName;
                mail.IsBodyHtml = false;
                mail.Body = ConfigurationManager.AppSettings["ApplicationName"].ToString() + " " + description + " " + url;
               
                /*
                 * For the daily notification you just need to compose an email to awaitingapprovaldb@sncorp 
                 * with the subject line being the SNC Employee Name 
                 * and the body containing a "application title" and then the links/descriptions for each approval needed.
                 */
                SmtpClient server = new SmtpClient();
                server.Send(mail);
            }
            catch (Exception ex)
            {
                log.Write("Error from EmailHandler::SendInformation stack: " + ex.ToString(), "Error");
            }
        }
    }
}
